﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace Tinh_toan_thong_so
{
    public partial class Form1 : Form
    {
        private float[] Mang_DL;
        private int TongSoMau;

        public Form1()
        {
            InitializeComponent();
        }

        //Summary:
        //        chon tap tin

        private void but_ChonTep_Click(object sender, EventArgs e)
        {
            string tenfile;
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.FileName = "KhaoSat.txt";
            dialog.InitialDirectory = Application.ExecutablePath;
            dialog.Filter = "txt files (*.txt)|*.txt";
            dialog.FilterIndex = 2;
            dialog.RestoreDirectory = true;
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                tenfile = dialog.FileName;
                textBox1.Text = Path.GetFullPath(dialog.FileName);
                StreamReader sr = new StreamReader(tenfile);
                try
                {
                    TongSoMau = int.Parse(sr.ReadLine());
                    string[] str;
                    Mang_DL = new float[TongSoMau];
                    for (int i = 0; i < TongSoMau; i++)
                    {
                        str = sr.ReadLine().Split(',');
                        Mang_DL[i] = float.Parse(str[1].ToString());
                    }
                    sr.Close();
                }
                catch
                {
                    sr.Close();
                }
                
            }
        }
        //Summary:
        //      tinh trung binh
        public float TrungBinh()
        {
            float TrungBinh = 0;
            for (int i = 0; i < TongSoMau; i++)
            {
                TrungBinh += Mang_DL[i];
            }
            return TrungBinh / TongSoMau;
        }
        
        
        //Summary:
        //      tính trung vi
        
        private void but_TrungVi_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < TongSoMau-1; i++)
            { 
                for(int j = i + 1; j< TongSoMau;j++)
                    if (Mang_DL[i] > Mang_DL[j])
                    {
                        float temp = Mang_DL[i];
                        Mang_DL[i] = Mang_DL[j];
                        Mang_DL[j] = temp;
                    }
            }
            if (this.TongSoMau % 2 == 0)
            {
                txt_KetQua.Text = ((Mang_DL[TongSoMau / 2] + Mang_DL[(TongSoMau / 2) - 1])/2).ToString();
            }
            else
            {
                int mau = TongSoMau / 2;
                txt_KetQua.Text = (Mang_DL[(mau)]).ToString();
            }
        }

        private void but_TrungBinh_Click_1(object sender, EventArgs e)
        {
            txt_KetQua.Text = TrungBinh().ToString();
        }

        //Summary:
        //      tinh skew
        
        private void but_Skew_Click(object sender, EventArgs e)
        {
            float TB = TrungBinh();
            double KetQua = 0;
            double Tu = 0;
            double Mau = 0;
            for (int i = 0; i < TongSoMau; i++ )
            {
                Tu += Math.Pow((i - TB), 3);
                Mau += Math.Pow((i - TB), 2);
            }
            KetQua = (double)(Tu / TongSoMau) / Math.Pow((Mau / TongSoMau),3/2) ;
            txt_KetQua.Text = KetQua.ToString();
        }

        //Summary:
        //      Max

        private float Max(float []a, int n)
        {
            float lc = a[0];
            for(int i =0;i<n;i++)
            {
                if (a[i] > lc)
                    lc = a[i];
            }
            return lc;
        }

        //Summary:
        //      min

        private float Min(float[] a, int n)
        {
            float lc = a[0];
            for (int i = 0; i < n; i++)
            {
                if (a[i] < lc)
                    lc = a[i];
            }
            return lc;
        }

        //Summary:
        //      tan suat

        private int Tansuat(float[] a, int n, float x)
        {
            int dem = 0;
            for (int i = 0; i < n; i++)
            {
                if (a[i] == x)
                    dem++;
            }
            return dem;
        }

        //Summary:
        //      tan suat lon nhat

        private int TanSuatLonNhat(float[] a, int n)
        {
            int lc = Tansuat(a, n, a[0]);
            for (int i = 0; i < n; i++)
            {
                  if (Tansuat(a, n, a[i]) > 1)
                  {
                      if (Tansuat(a, n, a[i]) > lc)
                          lc = Tansuat(a,n,a[i]);
                  }                
            }
            return lc;
        }

        //Summary:
        //      Sao chép mảng

        private void saoChepMang(float[] a, int n, float[] b, int m)
        {
            for(int i = 0; i< n; i++)
            {
                b[i] = a[i];
            }
        }

        private void xoaPhanTu(float[] a,ref int n ,int vt)
        {
            for (int i = vt; i < n-1; i++)
            {
                a[i] = a[i + 1];
            }
        }

        private void xoaNhieuPhanTu(float[] a,ref int n, float x)
        {
            for (int i = n - 1; i >= 0; i--)
            {
                if (a[i] == x)
                {
                    xoaPhanTu(a,ref n, i);
                    n--;
                }
            }
        }

        private void btMax_Click(object sender, EventArgs e)
        {
            txt_KetQua.Text = this.Max(this.Mang_DL, this.TongSoMau).ToString();
        }

        private void btMin_Click(object sender, EventArgs e)
        {
            txt_KetQua.Text = this.Min(this.Mang_DL, this.TongSoMau).ToString();
        }
        
        private void btYeuVi_Click(object sender, EventArgs e)
        {
            float[] b = new float[this.TongSoMau];
            saoChepMang(Mang_DL,TongSoMau,b,TongSoMau);
            int f = this.TanSuatLonNhat(b, this.TongSoMau);
            string str = "";
            bool trung = false;
            int m = this.TongSoMau;
            for (int i = 0; i < m; i++)
            {
                int yeuvi = this.Tansuat(b, m, b[i]);
                if(yeuvi == f && trung == false)
                {
                    str = b[i].ToString() + " là yếu vị với " + f.ToString() + " lần xuất hiện";
                    xoaNhieuPhanTu(b,ref m,b[i]);
                    i--;
                    trung = true;
                }
                else if(yeuvi == f && trung == true)
                {
                    str = "Khong co yeu vi";
                    break;
                }
            }
            txt_KetQua.Text = str;
        }


        private float GetVarience()
        {
            // tinh phuong sai mau
            float tb = TrungBinh();

            float result = 0;
            foreach (int i in Mang_DL)
            {
                result += (i - tb) * (i - tb);
            }
            result = (float)result / (Mang_DL.Length - 1);
            return result;
        }
        private double TinhDoLechChuan()
        {
            float phuongsai = GetVarience();

            return Math.Sqrt(phuongsai);
        }
        private double GetKurt()
        {
            //tinh tri kurt
            float tb = TrungBinh();
            double result = 0;
            double tu = 0;
            double mau = 0;
            foreach (int i in Mang_DL)
            {
                tu += Math.Pow((i - tb), 4);
                mau += Math.Pow((i - tb), 2);
            }
            result = (double)(tu / (Mang_DL.Length)) / ((mau / (Mang_DL.Length)) * (mau / (Mang_DL.Length)));
            return result;
        }

        private void btPhuongSai_Click(object sender, EventArgs e)
        {
            txt_KetQua.Text = GetVarience().ToString();
        }

        private void btLechChuan_Click(object sender, EventArgs e)
        {
            txt_KetQua.Text = TinhDoLechChuan().ToString();
        }

        private void btKurt_Click(object sender, EventArgs e)
        {
            txt_KetQua.Text = GetKurt().ToString();
        }

        private void btTuPhanVi_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < TongSoMau - 1; i++)
            {
                for (int j = i + 1; j < TongSoMau; j++)
                    if (Mang_DL[i] > Mang_DL[j])
                    {
                        float temp = Mang_DL[i];
                        Mang_DL[i] = Mang_DL[j];
                        Mang_DL[j] = temp;
                    }
            }
            string str = "";
            float meditan, q025, q075;
            if (this.TongSoMau % 2 == 0)
            {
                int mau = TongSoMau / 2;
                meditan = ((Mang_DL[mau] + Mang_DL[mau - 1]) / 2);
                if (mau % 2 == 1)
                {
                    q025 = Mang_DL[mau / 2];
                    q075 = Mang_DL[(mau / 2) + mau];
                }
                else
                {
                    q025 = (Mang_DL[(mau / 2) - 1] + Mang_DL[(mau / 2)]) / 2;
                    q075 = (Mang_DL[(mau / 2) + mau - 1] + Mang_DL[(mau / 2) + mau]) / 2;
                }
                
            }
            else
            {
                int mau = TongSoMau / 2;
                meditan = (Mang_DL[(mau)]);
                if (mau % 2 == 0)
                {
                    q025 = Mang_DL[mau / 2];
                    q075 = Mang_DL[(mau / 2) + mau];
                }
                else
                {
                    q025 = (Mang_DL[(mau / 2) + 1] + Mang_DL[(mau / 2)]) / 2;
                    q075 = (Mang_DL[(mau / 2) + mau + 1] + Mang_DL[(mau / 2) + mau]) / 2;
                }
            }
            str = "Meditan: " + meditan.ToString() + ",q0.25: " + q025.ToString() + ",q0.75: " + q075.ToString();
            txt_KetQua.Text = str;
        }

        private void btKCTuPhanVi_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < TongSoMau - 1; i++)
            {
                for (int j = i + 1; j < TongSoMau; j++)
                    if (Mang_DL[i] > Mang_DL[j])
                    {
                        float temp = Mang_DL[i];
                        Mang_DL[i] = Mang_DL[j];
                        Mang_DL[j] = temp;
                    }
            }
            string str = "";
            float q025, q075;
            if (this.TongSoMau % 2 == 0)
            {
                int mau = TongSoMau / 2;
                if (mau % 2 == 1)
                {
                    q025 = Mang_DL[mau / 2];
                    q075 = Mang_DL[(mau / 2) + mau];
                }
                else
                {
                    q025 = (Mang_DL[(mau / 2) - 1] + Mang_DL[(mau / 2)]) / 2;
                    q075 = (Mang_DL[(mau / 2) + mau - 1] + Mang_DL[(mau / 2) + mau]) / 2;
                }

            }
            else
            {
                int mau = TongSoMau / 2;
                if (mau % 2 == 0)
                {
                    q025 = Mang_DL[mau / 2];
                    q075 = Mang_DL[(mau / 2) + mau];
                }
                else
                {
                    q025 = (Mang_DL[(mau / 2) + 1] + Mang_DL[(mau / 2)]) / 2;
                    q075 = (Mang_DL[(mau / 2) + mau + 1] + Mang_DL[(mau / 2) + mau]) / 2;
                }
            }
            str = (q075 - q025).ToString();
            txt_KetQua.Text = str;
        }



    }
}
